import store from '@/store'
import * as types from '@/store/mutations_types'

// const base_url = 'http://127.0.0.1:5000'
const base_url = 'https://report.lswkdb.com'
// const base_url = 'http://192.168.10.143:5000'
// const base_url = 'http://192.168.199.233:5000'

const baseRequest = async (data) => {
  uni.showLoading({ title: '加载中' })
  let [error, res] = await uni.request(data)
  if (!res.data.success && res.data.code === 1001) {
    uni.hideLoading()
    uni.showModal({
      title: '错误',
      content: res.data.message,
      showCancel: false,
      success: (r) => {
        if (r.confirm) {
          store.commit(types.LOGOUT)
          uni.reLaunch({url: '/pages/users/login'})
        }
      }
    })
  }
  uni.hideLoading()
  return res.data
}


// 获取文章列表 获取自已的文章
export const fetchArticles = async (payload) => {
  return baseRequest({
    url: `${base_url}/api/articles?page=${payload.page}`,
    header: {'Authorization': `Bearer ${store.state.token['access_token']}`}
  })
}

// 查询文章列表 获取自已的文章
export const searchArticles = async (payload) => {
  return baseRequest({
    url: `${base_url}/api/articles/search?page=${payload.page}&text=${payload.text}`,
    header: {'Authorization': `Bearer ${store.state.token['access_token']}`}
  })
}

// 获取文章详情
export const fetchArticle = async (payload) => {
  return baseRequest({
    url: `${base_url}/api/article/${payload.id}`,
    header: { 'Authorization': `Bearer ${store.state.token['access_token']}` }
  })
}

// 更新文章
export const updateArticle = async (id, payload) => {
  return baseRequest({
    url: `${base_url}/api/article/${id}`,
    header: { 'Authorization': `Bearer ${store.state.token['access_token']}` },
    method: 'POST',
    data: payload
  })
}

// 管理员审核状态
export const auditArticle = async (id, payload) => {
  return baseRequest({
    url: `${base_url}/api/articles/audit/${id}`,
    header: { 'Authorization': `Bearer ${store.state.token['access_token']}` },
    method: 'POST',
    data: payload
  })
}

// 创建文章
export const addArticle = async (payload) => {
  return baseRequest({
    url: `${base_url}/api/articles`,
    header: { 'Authorization': `Bearer ${store.state.token['access_token']}` },
    method: 'POST',
    data: payload
  })
}


// 获取单位列表
export const fetchCompanys = async (payload) => {
  return baseRequest({
    url: `${base_url}/api/companys`
  })
}

// 获取专报列表
export const fetchReports = async (payload) => {
  return baseRequest({
    url: `${base_url}/api/reports`,
    header: { 'Authorization': `Bearer ${store.state.token['access_token']}` }
  })
}

// 获取专报详情
export const fetchReport = async (payload) => {
  return baseRequest({
    url: `${base_url}/api/reports/${payload.id}`
  })
}

// 获取栏目信息
export const fetchCategorys = async (payload) => {
  return baseRequest({
    url: `${base_url}/api/categorys`,
    header: { 'Authorization': `Bearer ${store.state.token['access_token']}` }
  })
}


// 删除指定附件
export const del_attachment = async (payload) => {
  return baseRequest({
    url: `${base_url}/api/articles/attachment/delete/${payload.id}`,
    method: 'DELETE',
    header: { 'Authorization': `Bearer ${store.state.token['access_token']}` }
  })
}

